package com.vehiclemanagement.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vehiclemanagement.entity.FuelCardRecharge;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;

/**
 * 加油卡充值记录Mapper接口
 */
@Mapper
public interface FuelCardRechargeMapper extends BaseMapper<FuelCardRecharge> {
    
    /**
     * 自定义分页查询，带加油卡信息
     * 
     * @param page 分页对象
     * @param whereSql 自定义WHERE条件SQL（不包含WHERE关键字）
     * @return 分页结果
     */
    @Select("SELECT r.*, f.card_number FROM fuel_card_recharge r " +
            "LEFT JOIN fuel_card f ON r.fuel_card_id = f.id " +
            "WHERE ${whereSql}")
    IPage<FuelCardRecharge> selectPageWithInfo(Page<FuelCardRecharge> page, @Param("whereSql") String whereSql);
    
    /**
     * 计算某张加油卡的总充值金额
     * 
     * @param fuelCardId 加油卡ID
     * @return 总充值金额
     */
    @Select("SELECT COALESCE(SUM(amount), 0) FROM fuel_card_recharge WHERE fuel_card_id = #{fuelCardId} AND status = 1 AND del_flag = 0")
    BigDecimal getTotalRechargeAmount(@Param("fuelCardId") Long fuelCardId);
    
    /**
     * 物理删除充值记录
     * 
     * @param id 充值记录ID
     * @return 影响行数
     */
    @Delete("DELETE FROM fuel_card_recharge WHERE id = #{id}")
    int deletePhysically(@Param("id") Long id);
} 